﻿using System;
using System.Windows.Forms;
using ApplicationGenerator.DataAccess;
using ApplicationGenerator.Lists.BusinessLogic;
using WeifenLuo.WinFormsUI.Docking;

namespace GUserInterface
{
    public delegate void ListSaved(string title);

    public partial class ListVEPanel : DockContent
    {
        #region events

        public event ListSaved ListSavedEvent;

        #endregion

        public ListVEPanel()
        {
            InitializeComponent();
            InitializeComponent_Custom();
        }

        public ListVEPanel( ListConfiguration listConfig )
        {
            InitializeComponent();
            listWizardConfigurationControl1.ConfigurationObject = listConfig;
            InitializeComponent_Custom();
        }

        public void InitializeComponent_Custom()
        {
            listWizardConfigurationControl1.Dock = DockStyle.Fill;
            listWizardConfigurationControl1.ShowFirstPage();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listWizardConfigurationControl1.ValidateConfiguration())
            {
                listWizardConfigurationControl1.SaveConfiguration();
                ListConfiguration listConfig = listWizardConfigurationControl1.ConfigurationObject;

                Guid id = Guid.NewGuid();
                if (false == ApplicationGenerator.DataAccess.Serializer.Serialize(Policy.List, listConfig, id, this.TabText))
                {
                    Serializer.Update(Policy.List, listConfig, this.TabText);
                }
                else
                {
                    ListSavedEvent(this.TabText);
                }

                this.Close();
            }
        }
    }
}
